package com.gopay.common.core.dic.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.core.dic.dao.SelectUtilDao;
import com.gopay.common.core.dic.vo.SelectVO;

@Repository("selectUtilDao")
public class SelectUtilDaoImpl extends BaseDAOImpl<Object[], Serializable> implements SelectUtilDao {

    @Override
    public List<SelectVO> getAllCountryAreaList() {
        String hql = "SELECT letterCode,chineseShortName,englishShortName FROM DicCbCountryAreaCode ";
        return getSelUnionVoList((List<Object[]>) findByHql(hql));
    }

    private List<SelectVO> getSelUnionVoList(List<Object[]> lo) {
        List<SelectVO> ls = new ArrayList<SelectVO>();
        if (lo != null && lo.size() > 0) {
            for (Object[] o : lo) {
                SelectVO sv = new SelectVO();
                sv.setCode((String) o[0]);
                sv.setName((String) o[1] + "|" + (String) o[2]);
                ls.add(sv);
            }
        }
        return ls;
    }

    private List<SelectVO> getSelVoList(List<Object[]> lo) {
        List<SelectVO> ls = new ArrayList<SelectVO>();
        if (lo != null && lo.size() > 0) {
            for (Object[] o : lo) {
                SelectVO sv = new SelectVO();
                sv.setCode((String) o[0]);
                sv.setName((String) o[1]);
                ls.add(sv);
            }
        }
        return ls;
    }

    @Override
    public List<SelectVO> getTradeTypeList() {
        String sql = "select t.code,t.name from DIC_CBRMB_TRADE_TYPE_QUATA t where del_flag = 0";
        return getSelVoList((List<Object[]>) findBySql(sql));
    }

    @Override
    public List<SelectVO> getTradeCodeList(String tradeTypeSq) {
        String sql = "select t.code,t.name from DIC_CBRMB_TRADE_TYPE_QUATA t" + " where t.trade_type=:trade_type "
                + "and t.code not in ('223022','223023') " + "and del_flag = 0 " + "order by t.sort_num asc";
        Map<String, Object> param = new HashMap<>();
        param.put("trade_type", "");
        if (tradeTypeSq.equals("hw")) {
            param.put("trade_type", "货物贸易");
        } else if (tradeTypeSq.equals("fw")) {
            param.put("trade_type", "服务贸易");
        }
        return getSelVoList((List<Object[]>) findBySql(sql, param));
    }

    @Override
    public List<SelectVO> getCarrierList() {
        String hql = "SELECT code, name FROM CbRmbCarrierInfo WHERE custId = '0'";
        List<Object[]> list = findByHql(hql);
        List<SelectVO> ls = new ArrayList<SelectVO>();
        if (list != null && list.size() > 0) {
            for (Object[] o : list) {
                SelectVO sv = new SelectVO();
                sv.setCode(o[0].toString());
                sv.setName(o[1].toString());
                ls.add(sv);
            }
        }

        return ls;
    }
}
